package com.example.controller;


import cn.hutool.json.JSONUtil;
import com.example.enums.ApiEnum;
import com.example.factory.SsopTagContext;
import com.example.model.SsopTag;
import com.example.service.SsopTagService;
import com.example.strategy.SsopTagStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liangzai
 * @since 2022-06-13
 */
@RestController
@RequestMapping("/tag")
@Slf4j
public class SsopTagController {

    @Autowired
    SsopTagService ssopTagService;


    @Autowired
    private ApplicationContext applicationContext;
    @Autowired
    SsopTagContext ssopTagContext;
    /**
     * 定义策略类
     *
     * @param
     * @return
     */
    @GetMapping("/getTag/{id}")
    public SsopTag getTagStrategy(@PathVariable("id") String id) {
        SsopTag tagObj = ssopTagService.getById(id);
        SsopTagStrategy ssopTagStrategy = applicationContext.getBean(tagObj.getTagGroup(), SsopTagStrategy.class);
        SsopTag resource = ssopTagStrategy.handleData(tagObj);
        log.info("返回结果" + JSONUtil.toJsonStr(resource));
        return resource;
    }

    /**
     * 通过枚举类获取
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    public SsopTag getTag(@PathVariable("id") String id) {
        SsopTag tagObj = ssopTagService.getById(id);
        //获取到枚举key对应的 beanName
        String servicecName = ApiEnum.getNameByType(tagObj.getName());
        SsopTagStrategy ssopTagStrategy = applicationContext.getBean(servicecName, SsopTagStrategy.class);
        SsopTag resource = ssopTagStrategy.handleData(tagObj);
        log.info("返回结果" + JSONUtil.toJsonStr(resource));
        return resource;
    }




    /**
     * 通过上下文context
     * @param id
     * @return
     */
    @GetMapping("/tag/{id}")
    public SsopTag tag(@PathVariable("id") String id) {
        SsopTag tagObj = ssopTagService.getById(id);
        SsopTag resource = ssopTagContext.getResource(tagObj);

         log.info("返回结果" + JSONUtil.toJsonStr(resource));
        return resource;
    }



}
